<?php
namespace app\home\controller;

use think\Controller;
use think\Db;

class Index extends Controller
{
    /**
     * @title 项目中心首页
     * @description 接口说明
     * @author jyh
     * @url /index
     * @method POST
     */
    public function index()
    {
        if ($this->request->isPost()) {
            $param = $this->request->post();

            // 需求 where条件
            $needWhere = "";
            // bug where条件
            $bugWhere = "";
            // 判断项目id是否存在 存在则加条件
            if (isset($param['project_id'])) {
                $needWhere .= "project_id = {$param['project_id']}";
                $bugWhere .= "project_id = {$param['project_id']}";
                $and = ' AND ';
            }
            // 判断用户id是否存在 存在则加条件
            if (isset($param['uid'])) {
                $needWhere .= "{$and}(front_user = {$param['uid']} OR after_user = {$param['uid']})";
                $bugWhere .= "{$and}assigner = {$param['uid']}";
            }
            // 查询需求 status打组
            $need = Db::name('projectNeed')->where($needWhere)->where('deleted', 'null')->group('status')->column('count(status)', 'status');
            // 查询bug status打组
            $bug = Db::name('projectBug')->where($bugWhere)->where('deleted', 'null')->group('status')->column('count(status)', 'status');
            // 查询项目
            $projectSum = Db::name('project')->where('status', 0)->where('deleted', 'null')->count();


            $needStatus = ['筹备中', '待开发', '开发中', '待测试', '测试中', '待上线', '已上线'];
            $bugStatus = ['未确认', '已确认', '已解决', '已关闭', '激活'];

            $needData = [];
            $bugData = [];
            $data['needSum'] = 0;
            $data['bugSum'] = 0;

            foreach ($needStatus as $key => $value) {
                if (isset($need[$key])) {
                    $data['needSum'] += $need[$key];
                    $needData[$key]['value'] = $need[$key];
                }else{
                    $needData[$key]['value'] = 0;
                }
                $needData[$key]['name'] = $needStatus[$key];
            }

            foreach ($bugStatus as $key => $value) {
                if (isset($bug[$key])) {
                    $data['bugSum'] += $bug[$key];
                    $bugData[$key]['value'] = $bug[$key];
                }else{
                    $bugData[$key]['value'] = 0;
                }
                $bugData[$key]['name'] = $bugStatus[$key];
            }

            $data['projectSum'] = $projectSum;
            $data['needData'] = $needData;
            $data['bugData'] = $bugData;

            return json(['code' => 0, 'msg' => '成功', 'data' => $data]);
        }
    }

    /**
     * @title 需求类型筛选
     * @description 接口说明
     * @author jyh
     * @url /home/index/needType
     * @method POST
     */
    public function needType()
    {
        if ($this->request->isPost()) {
            $param = $this->request->post();
            $where = [];
            // 判断项目id是否存在 存在则加条件
            if (isset($param['product_id'])) {
                $where['product_id'] = ['=',$param['product_id']];
            }
            // 判断版本id是否存在 存在则加条件
            if (isset($param['versions_id'])) {
                $where['versions_id'] = ['=',$param['versions_id']];
            }
            // 判断某个时间段是否存在 存在则加条件
            if (isset($param['time'])) {
                $where['ctime'][] = ['>=',$param['time'][0]];
                $where['ctime'][] = ['<=',$param['time'][1]];
            }
            // 查询需求以类型打组
            $type = Db::name('projectNeed')->where($where)->where('deleted', 'null')->group('type')->column('count(type)', 'type');

            $needType = Db::name('deploy')->where('fid','2')->field('id,name')->column('name','id');
            $typeData = [];

            foreach ($needType as $key => $value) {
                $typeData[$key]['value'] = isset($type[$key]) ? $type[$key] : 0;
                $typeData[$key]['name'] = $needType[$key];
            }
            $data['typeData'] = array_values($typeData);

            return json(['code' => 0, 'msg' => '成功', 'data' => $data]);
        }
    }

    /**
     * @title 统计前后端工时
     * @description 接口说明
     * @author jyh
     * @url /home/index/userInfo
     * @method POST
     */
    public function userInfo()
    {
        if ($this->request->isPost()) {
            $param = $this->request->post();
            $where = [];
            // 判断是否有天数
            if (isset($param['time'])) {
                // 获取$param['time']的天数
                $startDay = time() - $param['time'] * 24 * 3600;
                // 当前时间
                $endDay = time();
                //$where['rp1.ctime'][] = ['>=',$startDay];
                //$where['rp1.ctime'][] = ['<=',$endDay];
            }else{
                $startDay = time() - 7 * 24 * 3600;
                // 当前时间
                $endDay = time();
            }
            // 判断项目id是否存在 存在则加条件
            if (isset($param['project_id'])) {
                $where['pn.project_id'] = $param['project_id'];
            }
            $where['rp1.status'] = 1;
            $where['pn.deleted'] = null;
            $data = Db::name('ProjectNeed')
                ->alias('pn')
                ->join('RecordPlan rp1',"pn.`id` = rp1.`need_id` and rp1.status=1",'left')
                ->join('User u','u.`id` = rp1.`user_id`','left')
                ->where($where)
                ->field("FROM_UNIXTIME(rp1.`ctime`, '%m-%d') `ctime`,
                    CASE
                    WHEN pn.`front_user` = rp1.`user_id` THEN
                     FORMAT(SUM(CASE pn.`front_adjust` WHEN '0.0' THEN pn.`front_predict` ELSE pn.`front_adjust` END * rp1.`plan`/100),1)
                    WHEN pn.`after_user` = rp1.`user_id` THEN
                     FORMAT(SUM(CASE pn.`after_adjust` WHEN '0.0' THEN pn.`after_predict` ELSE pn.`after_adjust` END * rp1.`plan`/100),1)
                    ELSE
                     0
                    END AS `manhour`,pn.id,rp1.`user_id`,MAX(u.`name`) `name`")
                ->group("FROM_UNIXTIME(rp1.`ctime`, '%m-%d'),`user_id`")
                ->order('pn.ctime','desc')
                ->select();
            $day = [];
            // 取到传来的天数
            for ($i = $param['time']-1; $i >= 0; $i--) {
                $day[] = date("m-d",strtotime("-$i day"));
            }

            $userArr = [];
            // 合并用户数据，取到每天的工时
            foreach ($data as $key => $value) {
                $userArr[$value['name']][$value['ctime']] = $value['manhour'];
            }

            $userHour = [];
            // 处理数据
            foreach ($userArr as $key => $value) {
                foreach ($day as $k => $v) {
                    $userHour[$key]['name'] = $key;
                    $userHour[$key]['data'][] = isset($value[$v])? $value[$v] : 0;
                }
            }
            // 把当前数组变成有序数组
            $userHour = array_values($userHour);

            if ($data) {
                return json(['code' => 0, 'msg' => 'success', 'data' => $userHour, 'date' => $day]);
            } else {
                return json(['code' => 0, 'msg' => '无数据', 'data' => [], 'date' => $day]);
            }
        }
    }

    // 下载图片
    public function download() {
        $url = input('url','');
        $fileName = input('filename','qrcode');
        $file = explode('.',$fileName);
        $fileName = isset($file[1]) ? $fileName : $fileName.'.png';
        header('Content-Disposition:attachment;filename='.$fileName);
        readfile($url);
    }
}